// pages/organization/admin/modify/org_avatar.js
var app = getApp();
var util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    org_id: 0,
    org: [],
    org_avatar_preview: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var org_id = options.org_id;
    console.log(org_id);
    wx.setStorageSync("cutFlag", false);
    wx.setStorageSync('tempFilePaths', '');
    wx.setStorageSync('org_id', org_id);
    var own_orgs = wx.getStorageSync("own_orgs");
    var org = own_orgs[org_id];
    console.log(org);
    that.setData({
      org: org,
      org_id: org_id,
      org_avatar_preview: org.avatar_thumbnail_url
    });
    wx.setNavigationBarTitle({
      title: "管理组织"
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    
    var flag = wx.getStorageSync("cutFlag");
    var tempFilePaths = wx.getStorageSync("tempFilePaths")
    if (flag) {
      var own_orgs = wx.getStorageSync("own_orgs");
      var org_id = this.data.org_id;
      own_orgs[org_id].avatar_thumbnail_url = tempFilePaths;
      wx.setStorageSync("own_orgs", own_orgs);
      that.setData({
        org_avatar_preview: tempFilePaths
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 添加组织头像
  add_org_avatar: function () {

    var that = this;
    console.log('Now Add Org Avatar..');
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0]
        console.log(tempFilePaths);
        //org_avatar_preview = tempFilePaths;
        wx.setStorageSync('tempFilePaths', tempFilePaths)
        // 当获取图片成功时跳转到剪裁页面
        wx.navigateTo({
          url: '../../../cutInside/cutInside',
        })


        return;
      },
      // 获取图片失败则返回
      fail(res) {
        console.log("haha , ", res);

      }
    })
  },
  fixed_org_avatar_request(e) {
    var that = this;
    var yuyue_session = wx.getStorageSync("yuyue_session");
    var org_id = this.data.org_id;
    var tempFilePaths = wx.getStorageSync('tempFilePaths');
    var uploadTask = wx.uploadFile({//上传图片
      url: app.globalData.default_url + '/?action=api.v1.organization.setAvatar',
      filePath: tempFilePaths,
      name: 'file',
      formData: {
        'yuyue_session': yuyue_session,
        'org_id': (org_id),
      },
      success: function (res) {
        var data = res.data
        console.log(data);
        console.log(org_id);
        console.log(yuyue_session);
        data = JSON.parse(data);

        console.log(data);
        if (data.data.op != "fail") {
          wx.setStorageSync("org", data.data)
          wx.showModal({
            title: '修改成功',
            content: '修改组织头像成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({

                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '修改失败',
            content: data.data.reason,
            confirmText: "重试",
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                })
              } else {
                that.fixed_org_name_request(e);
                return;
              }
            }
          })
        }
      }, fail: function (res) {
        wx.showModal({
          title: '连接失败',
          content: "请检查网络连接",
          confirmText: "重试",
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
              })
            } else {
              that.fixed_org_name_request(e);
              return;
            }
          }
        })
      }
    })
    if (uploadTask)
      uploadTask.onProgressUpdate((res) => {
        console.log('上传res', res)
        console.log('上传进度', res.progress)
        console.log('已经上传的数据长度', res.totalBytesSent)
        console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
      });

  }
})